﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Smled
{
    public partial class ManagedBrushWizard : Form
    {
        private ManagedBrush brush = null;
        private bool ok = false;

        public ManagedBrushWizard()
        {
            InitializeComponent();
            tileChooser1.ChooserOrientation = Orientation.Horizontal;
            tileChooser1.Width = 607;
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                pbIlustration.Image = Properties.Resources.roadImg;
            }
            else
            {
                pbIlustration.Image = Properties.Resources.waterImg;
            }
        }

        public static bool ShowFor(ManagedBrush mb)
        {
            ManagedBrushWizard mbw = new ManagedBrushWizard();
            mbw.brush = mb;
            mbw.tileChooser1.Set = MainForm.Instance.MainView.Chooser.Set;
            mbw.textBox1.Text = mb.Name;
            mbw.ShowDialog(MainForm.Instance);
            //System.Diagnostics.Debug.WriteLine("Real dialog result : " + dr);
            return mbw.ok;
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            brush.Name = textBox1.Text;
            ok = true;

            if (radioButton1.Checked)
            {
                BrushRule r = new BrushRule(brush);
                r.Pattern[2, 1] = 1;
                r.Pattern[1, 2] = 1;
                r.Tiles.Add(mapView1.Map.GetTileAt(0, 0));
                brush.Rules.Add(r);

                r = new BrushRule(brush);
                r.Pattern[0, 1] = 1;
                r.Pattern[1, 2] = 1;
                r.Tiles.Add(mapView1.Map.GetTileAt(4, 0));
                brush.Rules.Add(r);

                r = new BrushRule(brush);
                r.Pattern[1, 0] = 1;
                r.Pattern[0, 1] = 1;
                r.Tiles.Add(mapView1.Map.GetTileAt(4, 4));
                brush.Rules.Add(r);

                r = new BrushRule(brush);
                r.Pattern[1, 0] = 1;
                r.Pattern[2, 1] = 1;
                r.Tiles.Add(mapView1.Map.GetTileAt(0, 4));
                brush.Rules.Add(r);

                r = new BrushRule(brush);
                r.Pattern[2, 1] = 1;
                r.Pattern[0, 1] = 1;
                r.Tiles.Add(mapView1.Map.GetTileAt(1, 0));
                r.Tiles.Add(mapView1.Map.GetTileAt(3, 0));
                r.Tiles.Add(mapView1.Map.GetTileAt(1, 2));
                r.Tiles.Add(mapView1.Map.GetTileAt(3, 2));
                r.Tiles.Add(mapView1.Map.GetTileAt(1, 4));
                r.Tiles.Add(mapView1.Map.GetTileAt(3, 4));
                brush.Rules.Add(r);

                r = new BrushRule(brush);
                r.Pattern[2, 1] = 1;
                r.Tiles.Add(mapView1.Map.GetTileAt(1, 0));
                r.Tiles.Add(mapView1.Map.GetTileAt(3, 0));
                r.Tiles.Add(mapView1.Map.GetTileAt(1, 2));
                r.Tiles.Add(mapView1.Map.GetTileAt(3, 2));
                r.Tiles.Add(mapView1.Map.GetTileAt(1, 4));
                r.Tiles.Add(mapView1.Map.GetTileAt(3, 4));
                brush.Rules.Add(r);

                r = new BrushRule(brush);
                r.Pattern[0, 1] = 1;
                r.Tiles.Add(mapView1.Map.GetTileAt(1, 0));
                r.Tiles.Add(mapView1.Map.GetTileAt(3, 0));
                r.Tiles.Add(mapView1.Map.GetTileAt(1, 2));
                r.Tiles.Add(mapView1.Map.GetTileAt(3, 2));
                r.Tiles.Add(mapView1.Map.GetTileAt(1, 4));
                r.Tiles.Add(mapView1.Map.GetTileAt(3, 4));
                brush.Rules.Add(r);

                r = new BrushRule(brush);
                r.Pattern[1, 0] = 1;
                r.Pattern[1, 2] = 1;
                r.Tiles.Add(mapView1.Map.GetTileAt(0, 1));
                r.Tiles.Add(mapView1.Map.GetTileAt(0, 3));
                r.Tiles.Add(mapView1.Map.GetTileAt(2, 1));
                r.Tiles.Add(mapView1.Map.GetTileAt(2, 3));
                r.Tiles.Add(mapView1.Map.GetTileAt(4, 1));
                r.Tiles.Add(mapView1.Map.GetTileAt(4, 3));
                brush.Rules.Add(r);

                r = new BrushRule(brush);
                r.Pattern[1, 2] = 1;
                r.Tiles.Add(mapView1.Map.GetTileAt(0, 1));
                r.Tiles.Add(mapView1.Map.GetTileAt(0, 3));
                r.Tiles.Add(mapView1.Map.GetTileAt(2, 1));
                r.Tiles.Add(mapView1.Map.GetTileAt(2, 3));
                r.Tiles.Add(mapView1.Map.GetTileAt(4, 1));
                r.Tiles.Add(mapView1.Map.GetTileAt(4, 3));
                brush.Rules.Add(r);

                r = new BrushRule(brush);
                r.Pattern[1, 0] = 1;
                r.Tiles.Add(mapView1.Map.GetTileAt(0, 1));
                r.Tiles.Add(mapView1.Map.GetTileAt(0, 3));
                r.Tiles.Add(mapView1.Map.GetTileAt(2, 1));
                r.Tiles.Add(mapView1.Map.GetTileAt(2, 3));
                r.Tiles.Add(mapView1.Map.GetTileAt(4, 1));
                r.Tiles.Add(mapView1.Map.GetTileAt(4, 3));
                brush.Rules.Add(r);

                r = new BrushRule(brush);
                r.Pattern[0, 1] = 1;
                r.Pattern[2, 1] = 1;
                r.Pattern[1, 2] = 1;
                r.Tiles.Add(mapView1.Map.GetTileAt(2, 0));
                brush.Rules.Add(r);

                r = new BrushRule(brush);
                r.Pattern[1, 0] = 1;
                r.Pattern[1, 2] = 1;
                r.Pattern[0, 1] = 1;
                r.Tiles.Add(mapView1.Map.GetTileAt(4, 2));
                brush.Rules.Add(r);

                r = new BrushRule(brush);
                r.Pattern[1, 0] = 1;
                r.Pattern[0, 1] = 1;
                r.Pattern[2, 1] = 1;
                r.Tiles.Add(mapView1.Map.GetTileAt(2, 4));
                brush.Rules.Add(r);

                r = new BrushRule(brush);
                r.Pattern[1, 0] = 1;
                r.Pattern[1, 2] = 1;
                r.Pattern[2, 1] = 1;
                r.Tiles.Add(mapView1.Map.GetTileAt(0, 2));
                brush.Rules.Add(r);

                r = new BrushRule(brush);
                r.Pattern[0, 1] = 1;
                r.Pattern[2, 1] = 1;
                r.Pattern[1, 0] = 1;
                r.Pattern[1, 2] = 1;
                r.Tiles.Add(mapView1.Map.GetTileAt(2, 2));
                brush.Rules.Add(r);

                brush.DefaultTile = mapView1.Map.GetTileAt(1, 0);
                brush.UpdateFromRules();
            }

            this.Close();
        }
    }
}
